package com.china08.yxyapi.api.home.activity.healthcompetition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionQues;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudent;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudentAnswer;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionQuesRepository;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentAnswerRepository;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentRepository;

/**
 * 做题(选择答案立即触发)
 * 
 * @author zd.Jin
 *
 *
 */
@RestController
@CrossOrigin
public class YxyCompetitionStudentAnswerCrtController extends YxyApiBaseController {
    @Autowired
    private YxyCompetitionQuesRepository yxyCompetitionQuesRepository;
    @Autowired
    private YxyCompetitionStudentRepository yxyCompetitionStudentRepository;
    @Autowired
    private YxyCompetitionStudentAnswerRepository yxyCompetitionStudentAnswerRepository;

    @RequestMapping(value = "/api_yw/home/activity/healthcompetition/studentanswer/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel respModel = new RespModel();
        String stageno = "";
        try {
            YxyCompetitionStudentAnswer yxyCompetitionStudentAnswer = yxyCompetitionStudentAnswerRepository
                    .findTop1ByStudentIdAndQuesOrder(reqModel.studentId, reqModel.quesOrder);
            YxyCompetitionStudent yxyCompetitionStudent = yxyCompetitionStudentRepository
                    .findTop1ByStudentId(reqModel.studentId);
            if (yxyCompetitionStudent != null) {
                // 展示题目(得到请求的题目)
                if ("小学".equals(yxyCompetitionStudent.getStage())) {
                    stageno = "01";
                } else if ("初中".equals(yxyCompetitionStudent.getStage())) {
                    stageno = "02";
                } else {
                    stageno = "03";
                }
                YxyCompetitionQues yxyCompetitionQues = yxyCompetitionQuesRepository.findTop1ByStageAndQuesNo(stageno,
                        reqModel.quesOrder);
                if (yxyCompetitionStudentAnswer == null) {
                    yxyCompetitionStudentAnswer = new YxyCompetitionStudentAnswer();
                    yxyCompetitionStudentAnswer.setQuesOrder(reqModel.quesOrder);
                    yxyCompetitionStudentAnswer.setStudentId(reqModel.studentId);
                    yxyCompetitionStudentAnswer.setMyAnswer(reqModel.myAnswer);
                    yxyCompetitionStudentAnswer.setQuesAnswer(yxyCompetitionQues.getQuesAnswer());
                    yxyCompetitionStudentAnswer.setPaperNum("");
                } else {
                    yxyCompetitionStudentAnswer.setMyAnswer(reqModel.myAnswer);
                }
                yxyCompetitionStudentAnswerRepository.save(yxyCompetitionStudentAnswer);
                yxyCompetitionStudent.setStatus(1);
                yxyCompetitionStudentRepository.save(yxyCompetitionStudent);
                respModel.notice = "修改成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            respModel.notice = "修改失败";
        }

        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String studentId;// 学生id

        public Integer quesOrder;// 题目order

        public String myAnswer;// 我的答案

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(studentId, null), "学生id不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(myAnswer, null), "我的答案不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        public String notice;
    }
}
